package com.lagou.controller;

import com.lagou.entity.Student;
import com.lagou.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class StudentController {

    @Resource
    private StudentService studentService;

    @GetMapping("/students")
    public List<Student> queryStudents() {
        return studentService.queryStudents();
    }

    @GetMapping("/student/{id}")
    public Student queryStudentById(@PathVariable("id") Integer id) {
        return studentService.findById(id);
    }

    @PostMapping("/student/add")
    public String addStudent(@RequestParam(value = "name") String name) {
        Student student = new Student();
        student.setName(name);
        studentService.add(student);
        return "success";
    }

    @PostMapping("/student/update")
    public String updateStudent(@RequestParam(value = "id") Integer id,
            @RequestParam(value = "name") String name) {
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        studentService.update(student);
        return "success";
    }

    @DeleteMapping("/student/{id}")
    public String deleteStudentById(@PathVariable("id") Integer id) {
        studentService.del(id);
        return "success";
    }
}
